package com.jyhg.core.common.utils
{
	/**
	 * DateUtil
	 * 日期操作
	 */
	public class DateUtil
	{
		public function DateUtil()
		{
		}
		
		/**
		 *  
		 * @param str 年x月x日
		 * 
		 */		
		public static function stringToDate(str:String):Date
		{
			var arr:Array = str.split(str.charAt(4));
			
			var date:Date = new Date(arr[0],arr[1]-1,arr[2]);//月份是0-11  除了年和日是从1开始
			
			return date;
		}
		
		/**
		 * 获得当前月的天数 
		 * @param d
		 * 
		 */
		public static function getDayInMonth(d:Date):int
		{
			const days:Array = [31,28,31,30,31,30,31,31,30,31,30,31];
			var day:int = days[d.month];
			if (isLeapYear(d.fullYear))
				day++;
			
			return day;
		}
		
		/**
		 * 是否是闰年
		 * @param year
		 * @return 
		 * 
		 */
		public static function isLeapYear(year:int):Boolean
		{
			if (year % 4 ==0)
				if (year % 400 == 0)
					if (year % 3200 == 0)
						if (year % 86400 == 0)
							return false;
						else
							return true;
						else 
							return false;
						else 
							return true;
						else
							return false;
		}
		
		
		/**
		 * 将倒计时秒数分割成yy:mm:dd格式
		 * @param second倒计时秒计时
		 * @return String 返回后的字符串，格式是yy:mm:dd
		 * 
		 * */
		public static function splitTime(second:uint,showHour:Boolean=true):String{
			var s:String="";
			var hour:int=Math.floor(second/(60*60));
			var minute:int=Math.floor(second%(60*60))/60;
			var sec:int=(Math.floor(second%(60*60))%60)%60;
			
			if(showHour){
				hour>9?s=s+hour:s=s+"0"+hour;
				s=s+":";
			}
			
			minute>9?s=s+minute:s=s+"0"+minute;
			s=s+":";
			
			sec>9?s=s+sec:s=s+"0"+sec;
			return s;
		}
		
		
		/**
		 * 根据字符串创建日期 
		 * (yy-mm-dd)
		 * @param v
		 * 
		 */
		public static function createDateFromString(v:String,timezone:Number = NaN):Date
		{
			v = v.replace(/-/g,"/");
			if (!isNaN(timezone))
			{
				var str:String = Math.abs(timezone).toString();
				if (str.length == 1)
					str = "0" + str;
				
				v = v + " GMT" + (timezone >= 0 ? "+" : "-") + str + "00";
			}
			return new Date(v);
		}
		
		/**
		 * 本地时间与通用时间的差 
		 * @return 
		 * 
		 */		
		public static function TimezoneOffset():int
		{
			var nextDay:Date = new Date();
			var offsetMilliseconds:Number = nextDay.getTimezoneOffset();			//本地时间和通用时间的差 单位是分钟
			return offsetMilliseconds;
		}
		
		/**
		 * 分转换成时间格式 65分 = 1:05;
		 * @param num
		 * @return 
		 * 
		 */		
		private static function transDate(num:Number):String{   
			
			var hour:Number = Math.abs(num/60);
			var minute:Number = Math.abs(num%60);
			
			var hStr:String = "";
			var mStr:String = "";
			if(hour<10)
			{
				hStr = "0"+hour;
			}
			else
			{
				hStr = ""+hour;
			}
			
			if(minute<10)
			{
				mStr = "0"+minute;
			}
			else
			{
				mStr = ""+minute;
			}
			return hStr+":"+mStr;
		}
	}
}
